package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.LineBaseCapacityValue;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface LineBaseCapacityValueDao {

    /**
     * 根据线体id查询
     *
     * @param pcbaLineId
     * @return
     */
    @Results(id = "lineBaseCapacityValueResultMap", value = {
            @Result(column = "line_base_capacity_value_id", property = "lineBaseCapacityValueId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_base_capacity_param_key", property = "lineBaseCapacityParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_base_capacity_param_name", property = "lineBaseCapacityParamName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_line_id", property = "pcbaLineId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_base_capacity_param_value", property = "lineBaseCapacityParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_base_capacity_param_value_audit", property = "lineBaseCapacityParamValueAudit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("select * from " + DB1TableName.LINE_BASE_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId}")
    List<LineBaseCapacityValue> getAllLineBaseCapacityValueByLineId(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 根据线体id + key 查询
     *
     * @param pcbaLineId
     * @param lineBaseCapacityParamKey
     * @return
     */
    @ResultMap(value = "lineBaseCapacityValueResultMap")
    @Select("select * from " + DB1TableName.LINE_BASE_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId} and line_base_capacity_param_key = #{lineBaseCapacityParamKey}")
    LineBaseCapacityValue getLineBaseCapacityValueByKeyAndLineId(@Param("pcbaLineId") Integer pcbaLineId, @Param("lineBaseCapacityParamKey") String lineBaseCapacityParamKey);

    /**
     * 根据线体id 删除
     *
     * @param pcbaLineId
     * @return
     */
    @Delete("delete from " + DB1TableName.LINE_BASE_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId}")
    Integer deleteLineBaseCapacityValueByLineId(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 插入
     *
     * @param lineBaseCapacityValue
     * @return
     */
    @Insert("insert into " + DB1TableName.LINE_BASE_CAPACITY_VALUE + "(line_base_capacity_param_key,line_base_capacity_param_name,pcba_line_id,line_base_capacity_param_value,create_time,line_base_capacity_param_value_audit) values " +
            "(#{lineBaseCapacityValue.lineBaseCapacityParamKey},#{lineBaseCapacityValue.lineBaseCapacityParamName},#{lineBaseCapacityValue.pcbaLineId},#{lineBaseCapacityValue.lineBaseCapacityParamValue},#{lineBaseCapacityValue.createTime},#{lineBaseCapacityValue.lineBaseCapacityParamValueAudit})")
    Integer addLineBaseCapacityValue(@Param("lineBaseCapacityValue") LineBaseCapacityValue lineBaseCapacityValue);

    /**
     * 更新
     *
     * @param lineBaseCapacityValue
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.LINE_BASE_CAPACITY_VALUE +
            "<set>" +
            "<if test='lineBaseCapacityValue.lineBaseCapacityParamKey != null and lineBaseCapacityValue.lineBaseCapacityParamKey != \"\" '>" +
            " line_base_capacity_param_key = #{lineBaseCapacityValue.lineBaseCapacityParamKey}," +
            "</if>" +
            "<if test='lineBaseCapacityValue.lineBaseCapacityParamName != null and lineBaseCapacityValue.lineBaseCapacityParamName != \"\" '>" +
            " line_base_capacity_param_name = #{lineBaseCapacityValue.lineBaseCapacityParamName}," +
            "</if>" +
            "<if test='lineBaseCapacityValue.pcbaLineId != null and lineBaseCapacityValue.pcbaLineId != \"\" '>" +
            " pcba_line_id = #{lineBaseCapacityValue.pcbaLineId}," +
            "</if>" +
            "<if test='lineBaseCapacityValue.lineBaseCapacityParamValue != null '>" +
            " line_base_capacity_param_value = #{lineBaseCapacityValue.lineBaseCapacityParamValue}," +
            "</if>" +
            "<if test='lineBaseCapacityValue.lineBaseCapacityParamValueAudit != null '>" +
            " line_base_capacity_param_value_audit = #{lineBaseCapacityValue.lineBaseCapacityParamValueAudit}," +
            "</if>" +
            "<if test='lineBaseCapacityValue.updateTime != null '>" +
            " update_time = #{lineBaseCapacityValue.updateTime}," +
            "</if>" +
            "</set>" +
            " where line_base_capacity_value_id = #{lineBaseCapacityValue.lineBaseCapacityValueId}" +
            "</script>")
    Integer updateLineBaseCapacityValue(@Param("lineBaseCapacityValue") LineBaseCapacityValue lineBaseCapacityValue);

    /**
     * 清空对应的临时值（审核通过）
     */
    @Update("update " + DB1TableName.LINE_BASE_CAPACITY_VALUE + " set line_base_capacity_param_value_audit = '' where pcba_line_id = #{pcbaLineId}")
    Integer clearValueAudit(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.LINE_BASE_CAPACITY_VALUE + " set line_base_capacity_param_value = line_base_capacity_param_value_audit where pcba_line_id = #{pcbaLineId}")
    Integer returnValueFromValueAudit(@Param("pcbaLineId") Integer pcbaLineId);
}
